package com.compomics.rover.gui;

import org.apache.log4j.Logger;

import com.compomics.rover.general.quantitation.RatioGroup;
import com.compomics.rover.general.singelton.QuantitativeValidationSingelton;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;

/**
 * Created by IntelliJ IDEA.
 * User: niklaas
 * Date: 9-mrt-2009
 * Time: 10:37:58
 * To change this template use File | Settings | File Templates.
 */
public class RatioGroupPanel {
    // Class specific log4j logger for RatioGroupPanel instances.
    private static Logger logger = Logger.getLogger(RatioGroupPanel.class);
    private JPanel jpanRatios;
    private JButton moreInfoButton;
    private JPanel jpanContent;
    private JSeparator multiSeparator;
    private JLabel lblMulti;
    private Connection iConnMsLims;
    private QuantitationValidationGUI iParent;
    /**
     * This validation singelton holds information for the calculation of the ratio
     */
    private QuantitativeValidationSingelton iQuantitativeValidationSingelton = QuantitativeValidationSingelton.getInstance();


    public RatioGroupPanel(final RatioGroup aRatioGroup, Connection aConnMsLims, QuantitationValidationGUI aParent) {
        this.iConnMsLims = aConnMsLims;
        this.iParent = aParent;

        $$$setupUI$$$();

        if (iQuantitativeValidationSingelton.isMultipleSources()) {
            //we're in mutli mode
            lblMulti.setText(iQuantitativeValidationSingelton.getTitles().get(aRatioGroup.getParentCollection().getIndex()));

        } else {
            multiSeparator.setVisible(false);
            lblMulti.setVisible(false);
        }

        //set background color
        if (aRatioGroup.isSelected()) {
            jpanContent.setBackground(Color.LIGHT_GRAY);
        } else {
            jpanContent.setBackground(Color.white);
        }
        moreInfoButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //if this button is pressed a new JFrame is created with extra information for this ratio
                RatioGroupInformationPanel lInfo = new RatioGroupInformationPanel(aRatioGroup, iConnMsLims, iParent);
                lInfo.setVisible(true);
            }
        });
        //show information for the different ratio types
        JPanel lTemp = new JPanel();
        lTemp.setLayout(new BoxLayout(lTemp, BoxLayout.Y_AXIS));
        lTemp.setBackground(Color.WHITE);
        for (int i = 0; i < aRatioGroup.getNumberOfRatios(); i++) {
            lTemp.add(Box.createHorizontalStrut(2));
            RatioPanel lRatioPanel = new RatioPanel(aRatioGroup.getRatio(i));

            JPanel lTemp2 = new JPanel();
            lTemp2.setLayout(new BoxLayout(lTemp2, BoxLayout.X_AXIS));
            lTemp2.setBackground(Color.WHITE);
            lTemp2.setBackground(Color.WHITE);
            lTemp2.add(Box.createHorizontalStrut(2));
            lTemp2.add(lRatioPanel.getContentPane());
            lTemp2.add(Box.createHorizontalGlue());

            lTemp.add(lTemp2);
        }
        jpanRatios.add(Box.createHorizontalStrut(2));
        jpanRatios.add(lTemp);
        jpanRatios.add(Box.createHorizontalGlue());
    }


    /**
     * Getter for the jpanContent
     *
     * @return JPanel
     */
    public JPanel getContentPane() {
        return this.jpanContent;
    }

    private void createUIComponents() {
        jpanRatios = new JPanel();
        jpanRatios.setLayout(new BoxLayout(jpanRatios, BoxLayout.X_AXIS));
        jpanRatios.setBackground(Color.WHITE);
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        jpanContent = new JPanel();
        jpanContent.setLayout(new GridBagLayout());
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        jpanContent.add(jpanRatios, gbc);
        moreInfoButton = new JButton();
        moreInfoButton.setBorderPainted(false);
        moreInfoButton.setContentAreaFilled(false);
        moreInfoButton.setFocusPainted(false);
        moreInfoButton.setIcon(new ImageIcon(getClass().getResource("/info.png")));
        moreInfoButton.setMargin(new Insets(3, 3, 3, 3));
        moreInfoButton.setText("");
        moreInfoButton.setToolTipText("more info");
        gbc = new GridBagConstraints();
        gbc.gridx = 4;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(0, 2, 0, 2);
        jpanContent.add(moreInfoButton, gbc);
        multiSeparator = new JSeparator();
        multiSeparator.setOrientation(1);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 2, 5, 2);
        jpanContent.add(multiSeparator, gbc);
        final JSeparator separator1 = new JSeparator();
        separator1.setOrientation(1);
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 2, 5, 2);
        jpanContent.add(separator1, gbc);
        lblMulti = new JLabel();
        lblMulti.setText("");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        jpanContent.add(lblMulti, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return jpanContent;
    }
}
